import { OnInit, Component, Input, ViewChild } from '@angular/core';
import { QueryOptions } from 'src/app/project/model/common/query-options';
import { ObjectUtil } from 'src/app/project/utils/object-util';
import { TABLE_PARAMETER } from '../../setting/table-parameter';
import { STColumn } from '@delon/abc';
import { ConfigurationInfoDTO } from 'src/app/project/model/icm/icm-global/configuration/configuration-info.dto';
import { ConfigurationService } from 'src/app/project/service/icm/icm-global/configuration/configuration.service';
import { NzMessageService } from 'ng-zorro-antd';
import { ConfigurationDetailComponent } from './configuration-detail.component';

@Component({
  selector: 'app-configuration-list',
  templateUrl: './configuration-list.component.html',
  styles: [],
})
export class ConfigurationListComponent implements OnInit {
  constructor(private configurationService: ConfigurationService, private msg: NzMessageService) {}

  @ViewChild('configurationDetailComponent', { static: false })
  configurationDetailComponent: ConfigurationDetailComponent;

  /**
   * 分页参数初始化
   */
  queryOptions: QueryOptions = {
    page: 0,
    size: 20,
    sort: 'id,asc',
  };

  /**
   * 查询参数
   */
  @Input()
  queryParams;

  /**
   * 请求数据标识
   */
  loading = false;

  /**
   * 表格数据
   */
  tableData: Array<ConfigurationInfoDTO> = [];

  /**
   * 列表参数
   */
  TABLE_PARAMETER = ObjectUtil.deepClone(TABLE_PARAMETER);

  /**
   * 表格的列定义
   */
  stColumns: STColumn[] = [
    {
      title: '序号',
      render: 'number',
      width: '2%',
      className: 'text-center',
    },
    {
      title: '配置名称',
      index: 'name',
      width: '4%',
      className: 'text-center',
    },
    {
      title: '配置值',
      index: 'value',
      width: '4%',
      className: 'text-left',
    },
    {
      title: '是否可用',
      render: 'available',
      width: '4%',
      className: 'text-center',
    },
    {
      title: '显示顺序',
      index: 'showOrder',
      width: '3%',
      className: 'text-center',
      sort: this.TABLE_PARAMETER.sortDef,
    },
    {
      title: '备注',
      index: 'remark',
      width: '5%',
      className: 'text-left',
    },
    {
      title: '操作',
      render: 'operations',
      index: 'operation',
      width: '4%',
      className: 'text-center',
      fixed: 'right',
    },
  ];

  /**
   * 表格变化事件，用于双击、排序，翻页等操作
   * @param e event
   */
  change(e: any): void {
    // 双击事件
    if (e.type === 'dblClick') {
      this.loading = true;
      // tslint:disable-next-line: deprecation
      this.configurationService.findById(e.dblClick.item.id).subscribe(
        data => {
          this.configurationDetailComponent.edit(true, data);
        },
        null,
        () => {
          this.loading = false;
        },
      );
    }
    // 排序事件
    if (e.type === 'sort') {
      if (e.sort.map) {
        const temp = e.sort.map.sort;
        if (temp) {
          this.queryOptions.sort = temp.replace(/[\.-]/g, ',');
        }
      }
      this.loadData();
    }
    // 翻页设置
    if (e.type === 'ps' || e.type === 'pi') {
      this.queryOptions.page = e.pi - 1;
      this.queryOptions.size = e.ps;
      this.loadData();
    }
  }

  /**
   * 获取全局配置数据
   */
  loadData() {
    this.loading = true;
    // tslint:disable-next-line: deprecation
    this.configurationService.findOnePage(this.queryOptions, this.queryParams.name).subscribe(
      data => {
        if (data) {
          this.tableData = data.data;
          this.TABLE_PARAMETER.page.total = data.totalRecords;
        }
      },
      null,
      () => {
        this.loading = false;
      },
    );
  }

  /**
   * 新建字典
   */
  create() {
    this.configurationDetailComponent.edit(false, null);
  }

  /**
   * 修改配置信息
   * @param row 参数配置数据
   */
  onUpdate(row: ConfigurationInfoDTO) {
    this.loading = true;
    // tslint:disable-next-line: deprecation
    this.configurationService.findById(row.id).subscribe(
      data => {
        this.configurationDetailComponent.edit(false, data);
      },
      null,
      () => {
        this.loading = false;
      },
    );
  }

  /**
   * 删除系统参数
   */
  onDelete(row: ConfigurationInfoDTO) {
    this.configurationService.delete(row.id).subscribe(
      () => {
        this.msg.success('删除成功');
        this.loadData();
      },
      () => this.msg.error('删除失败'),
      () => this.loadData(),
    );
  }

  ngOnInit() {
    this.loadData();
  }
}
